#include <linux/module.h>
#include <linux/printk.h>
#include <linux/major.h>
#include <linux/gpio.h>             // gpio_set_value() & gpio_get() & gpio_free()
#include <linux/uaccess.h>			//  copy_to_user() & copy_from_user
#include <linux/gpio/consumer.h>
#include <linux/platform_device.h>
#include <linux/gpio/consumer.h>    //  struct gpio_desc 


/* 1. 确定主设备号 */
static int major = 0;
static struct class *led_class;
static struct gpio_desc *led_gpio;

static ssize_t led_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
    printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);

    return 0;
}

static ssize_t led_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
    int err;
    char status;
    //struct inode *inode = file_inode(file);
    //int minor = iminor(inode);

    printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    err = copy_from_user(&status, buf, 1);

    /* 根据次设备号和 status 控制 LED */
    gpiod_set_value(led_gpio, status);

    return 1;
}

static int led_drv_open (struct inode *node, struct file *file)
{
    //int minor = iminor(node);

    gpiod_direction_output(led_gpio, 0);

    return 0;
}

static int led_drv_close (struct inode *node, struct file *file)
{
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	return 0;
}

/* 定义自己的 file_operations 结构体 */
static struct file_operations led_drv = {
    .owner = THIS_MODULE,
    .open = led_drv_open,
    .read = led_drv_read,
    .write = led_drv_write,
    .release = led_drv_close,
};

/* 从 platform_device 获取 gpio 把从 file_operations 结构体告诉内核：注册驱动程序 */
static int chip_demo_gpio_probe(struct platform_device *pdev)
{
    //int err;

    /* 设备树中定义有：led-gpios=<...>; */
    led_gpio = gpiod_get(&pdev->dev, "led", 0);
    if (IS_ERR(led_gpio)) {
        dev_err(&pdev->dev, "Failed to get GPIO for led\n");
        return PTR_ERR(led_gpio);
    }

    /* 注册 file_operations */
    major = register_chrdev(0, "rk3399_led", &led_drv); /* /dev/rk3399_led */

    led_class = class_create(THIS_MODULE, "rk3399_led_class");
    if (IS_ERR(led_class)) {
        printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
        unregister_chrdev(major, "led");
        gpiod_put(led_gpio);
        return PTR_ERR(led_class);
    }

    device_create(led_class, NULL, MKDEV(major, 0), NULL, "rk3399_led%d", 0); /* /dev/rk3399_led0 */

    return 0;
}

static int chip_demo_gpio_remove(struct platform_device *pr_deve)
{
    device_destroy(led_class, MKDEV(major, 0));
    class_destroy(led_class);
    unregister_chrdev(major, "100ask_led");
    gpiod_put(led_gpio);

    return 0;
}

static const struct of_device_id rk3399_leds[] = {
    { .compatible = "rk3399,leddrv"},
    { },
};

/* 定义 platform_driver*/
static struct platform_driver chip_demo_gpio_driver = {
    .probe = chip_demo_gpio_probe,
    .remove = chip_demo_gpio_remove,
    .driver = {
        .name = "rk3399_led",
        .of_match_table = rk3399_leds,
    },
};

/* 2. 在入口函数中注册 platform_driver */
static int __init led_init(void)
{
    int err = 0;

    printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);

    err = platform_driver_register(&chip_demo_gpio_driver);

    return err;
}

/* 3. 有了入口函数就应该有出口函数：卸载驱动程序时，就会调用这个出口函数卸载 platform_driver */
static void __exit led_exit(void)
{
    printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);

    platform_driver_unregister(&chip_demo_gpio_driver);
}


module_init(led_init);
module_exit(led_exit);

MODULE_LICENSE("GPL");